package com.xhwl.centiotdata.service.guangtuo;

import com.xhwl.centiotdata.suppliersystem.guangtuo.DeviceGuangTuoAlarmHostHandler;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.alarmhost.AlarmHostInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhouyongzhou
 * @date 2023/04/26  9:24
 */
@Slf4j
@Service
public class AlarmHostServiceImpl implements AlarmHostService {
    @Value("${guangTuo.alarmhost.ipAndPort:-1}")
    private String ipAndPort;
    @Resource
    private DeviceGuangTuoAlarmHostHandler deviceGuangTuoAlarmHostHandler;

    @Override
    public ResultJson batchPutDefAreaCtrlType(AlarmHostInfoDTO alarmHost) {
        if (null==ipAndPort || ipAndPort.length()==0 || ipAndPort.equals("-1")){
             return ResultJson.fail("广拓报警主机的ip端口不能为空");
        }
        List<String> objIdList=new ArrayList<>();
        for (String objID : alarmHost.getObjIDList()) {
            boolean ctrlResult = deviceGuangTuoAlarmHostHandler.putDefAreaCtrlType(ipAndPort, objID, alarmHost.getCtrlType());
            if (ctrlResult){
                objIdList.add(objID);
            }
        }
        return ResultJson.success(objIdList);
    }


    @Override
    public ResultJson putSubSystemCtrlType(AlarmHostInfoDTO alarmHost) {
        if (null==ipAndPort || ipAndPort.length()==0 || ipAndPort.equals("-1")){
            return ResultJson.fail("广拓报警主机的ip端口不能为空");
        }
        boolean ctrlResult = deviceGuangTuoAlarmHostHandler.putSubSystemCtrlType(ipAndPort,alarmHost.getBelongSubSysNo(), alarmHost.getCtrlType());
        return ctrlResult?ResultJson.success() : ResultJson.fail("修改成功");
    }
}
